﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CNTVNow.Web.Helpers.Common;

namespace CNTVNow.Web.Models
{
    public class NewsModel
    {
        public List<New> GetNewsTopBanner(int menuId, int parentId)
        {
            return (from n in SingletonContext.CntvContext.News
                    where (n.NewsCategoryID == menuId) && (n.SubPagePositionID == Config.PositionTopBanner()) && (n.Status == true) 
                    orderby n.LastModifiedDate descending 
                    select n).Take(Config.NewsTopNumber(menuId,parentId)).ToList();
        }
        public List<New> GetNewsUnderTopBanner(int menuId, int parentId)
        {
            return (from n in SingletonContext.CntvContext.News
                    where (n.NewsCategoryID == menuId) && (n.SubPagePositionID == Config.PositionUnderTopBanner()) && (n.Status == true) 
                    orderby n.LastModifiedDate descending
                    select n).Take(Config.NewsUnderTopNumber(menuId, parentId)).ToList();
        }
        public List<New> GetNewsLatest(int menuId, int parentId)
        {
            return (from n in SingletonContext.CntvContext.News
                    where (n.NewsCategoryID == menuId) && (n.SubPagePositionID == Config.PositionLatestNews()) && (n.Status == true) 
                    orderby n.LastModifiedDate descending
                    select n).Take(Config.NewsLatestNumber(menuId, parentId)).ToList();
        }
        public List<New> GetNewsLatest(int menuId, int parentId,int pageIndex)
        {
            return (from n in SingletonContext.CntvContext.News
                    where (n.NewsCategoryID == menuId) && (n.SubPagePositionID == Config.PositionLatestNews()) && (n.Status == true) 
                    orderby n.LastModifiedDate descending
                    select n).Skip(Config.NewsLatestNumber(menuId, parentId) * pageIndex).Take(Config.NewsLatestNumber(menuId, parentId)).ToList();
        }
        public List<New> GetNewsLatest(int menuId, int parentId, int type, int pageIndex )
        {
            if (type == 1)
            {
                return (from n in SingletonContext.CntvContext.News
                        where (n.NewsCatParentID == menuId) && (n.SubPagePositionID == Config.PositionLatestNews()) && (n.NewsCategoryID == type) && (n.Status == true) 
                        orderby n.LastModifiedDate descending
                        select n).Skip(Config.NewsLatestNumber(menuId, parentId) * pageIndex).Take(Config.NewsLatestNumber(menuId, parentId)).ToList();
            }
            else
            {
                return (from n in SingletonContext.CntvContext.News
                        where (n.NewsCatParentID == menuId) && (n.SubPagePositionID == Config.PositionLatestNews()) && (n.NewsCategoryID == type)
                        orderby n.LastModifiedDate descending
                        select n).Skip(Config.NewsLatestNumber(menuId, parentId) * pageIndex).Take(Config.NewsLatestNumber(menuId, parentId)).ToList();
            }
        }
        public List<New> GetNewsLeft(int menuId, int parentId)
        {
            return (from n in SingletonContext.CntvContext.News
                    where (n.NewsCategoryID == menuId) && (n.SubPagePositionID == Config.PositionLeftNews()) && (n.Status == true) 
                    orderby n.LastModifiedDate descending
                    select n).Take(Config.NewsLeftNumber(menuId, parentId)).ToList();
        }
        public List<New> GetNewsMiddle(int menuId, int parentId)
        {
            return (from n in SingletonContext.CntvContext.News
                    where (n.NewsCategoryID == menuId) && (n.SubPagePositionID == Config.PositionMiddleNews()) && (n.Status == true) 
                    orderby n.LastModifiedDate descending
                    select n).Take(Config.NewsMiddleNumber(menuId, parentId)).ToList();
        }
        public List<AdvertisementItem> GetAdsItem(int menuId, int AdsPosition)
        {
            return (from ads in SingletonContext.CntvContext.AdvertisementItems
                    where (ads.AdvCategoryID == menuId) && (ads.AdvertisementPosition.Id == AdsPosition)
                            && (ads.StartTime <= DateTime.Now) && (ads.EndTime >= DateTime.Now)
                    orderby ads.Created_At descending
                    select ads).Take(Config.AdsMax()).ToList();
        }
        public New GetNewDetail(int newsId)
        {
            var query =  (from n in SingletonContext.CntvContext.News where n.NewsID == newsId select n).ToList();
            if (query.Count > 0)
                return query.First();
            else
                return null;
        }
        public List<New> GetNewsByCategory(int categoryId, DateTime datePosted)
        {
            return (from n in SingletonContext.CntvContext.News 
                    where n.NewsCategoryID == categoryId && (n.Status == true) && (n.CreatedDate <= datePosted)
                    select n).Take(FixCommon.NewsNumberDetail).ToList();
        }
        public List<New> GetNewsBySearchByCategory(string name, int categoryId)
        {
            return (from n in SingletonContext.CntvContext.News
                    where n.NewsCategoryID == categoryId && (n.Status == true) && (n.NewsTitle.IndexOf(name) >= 0)
                    select n).Take(FixCommon.NewsNumberDetail).ToList();
        }
        public List<New> GetNewsBySearchFull(string name)
        {
            return (from n in SingletonContext.CntvContext.News
                    where  (n.Status == true) && (n.NewsTitle.IndexOf(name) >= 0)
                    select n).Take(FixCommon.NewsNumberDetail).ToList();
        }
    }
}